/**
 * COPYRIGHT (C) 2008 Erica Margetich and Loc Phan 
 * To create a Decode Substitution Cipher Button for Security Viewer
 * @author Erica Margetich
 * @author Loc Phan
 * @version 1.01 2008-12-07
 */

package edu.sjsu.cs.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import edu.sjsu.cs.security.SubstitutionCipher;

public class DecodeSubstitutionCipherButton extends JButton
{
   /**
    * Construct a decode substitution cipher button
    * @param aTextfield Enter a key text field
    * @param keyTextField Enter the amount of times it will shift
    */
   public DecodeSubstitutionCipherButton(JTextField aTextfield,JTextField keyTextField)
   {
      super("Decode with Substitution Cipher");
      TextField = aTextfield;
      KeyTextField = keyTextField;
      ActionListener listener = new ButtonListener();
      this.addActionListener(listener);
   }
   class ButtonListener implements ActionListener
   {
      /**
       * The action performed when the button is pressed
       * @param e the action event that is performed
       * @exception e1 must enter positive integer
       */
      public void actionPerformed(ActionEvent e)
      {
         try
         {
            Key = Integer.parseInt(KeyTextField.getText());
            SubstitutionCipher encryption = new SubstitutionCipher(Key);
            Label.setText(encryption.decode(TextField.getText()));
         }
         catch(Exception e1)
         {
            Label.setText("Please enter a positive integer smaller than "+Integer.MAX_VALUE+" .");
         }
      }
   }
   /**
    * Create a JLabel for the Decode Substitution Cipher Button
    * @param aLabel the label for the button
    */
   public void addLabel(JLabel aLabel)
   {
      Label = aLabel;
   }
   //private instance fields
   private JLabel Label;
   private JTextField TextField;
   private JTextField KeyTextField;
   private int Key;
}

